//
//  SDGuideViewController.m
//  SuperDoctor
//
//  Created by 龚睿 on 16/7/11.
//  Copyright © 2016年 SD. All rights reserved.
//

#import "SDGuideViewController.h"
#import "SDGuideCell.h"


@interface SDGuideViewController ()<UIScrollViewDelegate>


//存取图片的数组
@property(nonatomic,strong)NSArray<UIImage *> *imgsArr;


// 记录
@property(nonatomic,assign)int currentPage;
@end

@implementation SDGuideViewController

static NSString * const reuseIdentifier = @"Cell";


#pragma mark
#pragma mark -重写init方法指定流水布局
- (instancetype)init{

// MARK: - 创建流水布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
//    1.2设置cell的大小
    flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
//    设置item的间距
    flowLayout.minimumLineSpacing = 0;
//    设置滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    return [super initWithCollectionViewLayout:flowLayout];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = [UIColor colorWithRed:255/256.0 green:255/256.0 blue:255/256.0 alpha:1];
//    关闭弹簧效果
    self.collectionView.bounces = NO;
    
//    设置分页效果
    self.collectionView.pagingEnabled = YES;
    
//    关闭底部的滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    
    [self.collectionView registerClass:[SDGuideCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
   }


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.imgsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SDGuideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
//    MARK: -设置cell的背景图片
    cell.backgroundView = [[UIImageView alloc] initWithImage:self.imgsArr[indexPath.item]];
    
    cell.currentPageIndex = self.currentPage;
    
    //     最后一个cell现实按钮,其他的隐藏!
    if (indexPath.item != self.imgsArr.count - 1) {
        
        [cell.experienceBtn setTitle:@"跳过" forState:UIControlStateNormal];
        
    }else{
        [cell.experienceBtn setTitle:@"子曰" forState:UIControlStateNormal];
        
    }
    return cell;
}


#pragma mark
#pragma mark -


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    int index = round(self.collectionView.contentOffset.x/self.collectionView.frame.size.width) ;
    
    self.currentPage = index ;
    
    [self.collectionView reloadData];

}

#pragma mark
#pragma mark - 数组懒加载
- (NSArray<UIImage *> *)imgsArr{

    if (_imgsArr == nil) {
        _imgsArr = [NSArray array];
        _imgsArr = @[[UIImage imageNamed:@"XO1"],[UIImage imageNamed:@"XO2"],[UIImage imageNamed:@"XO3"],[UIImage imageNamed:@"XO4"],[UIImage imageNamed:@"XO5"]];
    }

    return _imgsArr;
}



@end
